var api = require('../../utils/api.js');
var app = getApp();
Page({
    data: {
        systemInfo: {},
        _api: {},
        list: [],
        total: 0,
        loadingMore: false,
        noMoreData: false,
        user_id:'',//用户ID
        pwd: '',//视频密码
        has_pwd: false,// false没有密码 true有密码
        verify_pwd: false,//判断输入密码是否正确
        focus: true,
    },
    currentPageNumber: 1,
    onLoad() {
        try {
            var isLogin = wx.getStorageSync('login');
            if (!isLogin) {
                api.login();
                return;
            }
        } catch (e) {
            // Do something when catch error
        }

        this.setData({
            _api: api,
        });

    },
    onShow() {
        // console.log(app.pagesNeedUpdate);
        // if (app.pagesNeedUpdate['pages/index/index'] == 1) {
        //     let newItems = api.updatePageList('id');
        //     console.log(newItems);
        //     this.setData({
        //         list: newItems
        //     });
        // }
        //
        // if (app.pagesNeedUpdate['pages/index/index'] == 'refresh') {
        //     this.onPullDownRefresh();
        // }
        this.pullUpLoad();

        // api.pageNeedUpdate('pages/index/index', 0);
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh() {
      let that = this;
        this.currentPageNumber = 1;
        this.setData({
            noMoreData: false,
            noData: false
        });
        api.get({
          url: 'user/favorites/wx_favorites',
            data: {
              page: this.currentPageNumber,
              user_id: that.data.user_id
            },
            success: data => {
                let newItems = api.updatePageList('id', data.data, this.formatListItem, true);
                console.log(newItems);
                this.setData({
                    list: newItems
                });

                if (data.data.length > 0) {
                    this.currentPageNumber++;
                } else {
                    this.setData({
                        noMoreData: true
                    });

                    // 没有数据
                    // this.setData({
                    //     noMoreData: true,
                    //     noData: true
                    // });
                }

                wx.stopPullDownRefresh();
            }
        });
    },

    /**
     * 上拉刷新
     */
    pullUpLoad() {
        let that =this;
        if (this.data.loadingMore || this.data.noMoreData) return;
        var user = wx.getStorageSync('user');//用户信息
        this.setData({
          user_id: user.id,
          loadingMore: true
        });
        wx.showNavigationBarLoading();
        console.log(user.id)
        api.post({
          url: 'user/favorites/wx_favorites',
            data: {
                // page: this.currentPageNumber,
                user_id: user.id
            },
            success: data => {
                let newItems = api.updatePageList('id', data.data, this.formatListItem);
                console.log(newItems);
                this.setData({
                    list: newItems
                });
                if (data.data.length > 0) {
                    this.currentPageNumber++;
                } else {
                    this.setData({
                        noMoreData: true
                    });
                }
            },
            complete: () => {
                this.setData({
                    loadingMore: false
                });
                wx.hideNavigationBarLoading();
            }
        });
    },
    formatListItem(item) {
        if (item.Thumbnail) {
            item.Thumbnail = api.getUploadUrl(item.Thumbnail);
        }
        return item;
    },
    onReachBottom() {
        this.pullUpLoad();
    },
    //弹出框 取消按钮
    onCancel: function () {
      this.setData({
        has_pwd: false,
        focus: true,
      });
    },
    //弹出框 确定按钮
    onConfirm() {
      var that = this;
      console.log(this.data.pwd_input)
      console.log(this.data.pwd)

      if (this.data.pwd_input == this.data.pwd) {
        this.setData({
          has_pwd: false,
          focus: true,
        });
        wx.navigateTo({
          url: '/pages/article/article?id=' + this.data.art_id
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '密码错误',
          showCancel: false,
          success: function (res) {
            console.log(res)
            if (res.confirm) {
              that.setData({
                has_pwd: true,
                focus: true
              })
            }
          }
        })
        return
      }
    },
    priceInput(e) {
      this.setData({
        pwd_input: e.detail.value
      })
    },
    onListItemTap(e) {
      let id = e.currentTarget.dataset.id;

      //判断该课程是否有密码
      api.post({
        url: 'wxapp/Index/isset_pwd',
        data: {
          id: id
        },
        success: res => {
          if (res.data.post_pwd) {
            this.setData({
              art_id: id,
              pwd: res.data.post_pwd,
              has_pwd: true
            })
          } else {
            wx.navigateTo({
              url: '/pages/article/article?id=' + id
            });
          }
        }
      })
    },
    deleteArticles: function (e) {
        let id = e.currentTarget.dataset.id;
        console.log(id);
        wx.showActionSheet({
            itemList: ['取消收藏'],
            success: (res) => {
                console.log(res.tapIndex)
                api.delete({
                    url: 'user/favorites/' + id,
                    data: {},
                    success: data => {
                        if (data.code) {
                            let newItems = api.deletePageListItem(id);
                            console.log(newItems);
                            this.setData({
                                list: newItems
                            });
                            wx.showToast({
                                title: '取消成功!',
                                icon: 'success',
                                duration: 1000
                            });
                        } else {
                            wx.showToast({
                                title: data.msg,
                                icon: 'success',
                                duration: 1000
                            });
                        }

                    },
                    complete: () => {

                    }
                });
            },
            fail: function (res) {
                console.log(res.errMsg)
            }
        });
    },
    //分享
    onShareAppMessage(e) {
      let user_id = this.data.user_id
      return {
        path: '/pages/index/index',
        success: function (res) {
          api.post({
            url: 'wxapp/Index/share',
            data: {
              title: '收藏页',
              user_id: user_id
            },
            success: res => {
              wx.showToast({
                title: res.msg,
                icon: 'success',
                duration: 2000
              })
            }
          })
        },
      }
    },

});
